package com.yunxi.controller.sto;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sto.pojo.*;
import com.yunxi.sto.service.StoStockInPlanDubboService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 车辆入库计划 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-05-17
 */
@RestController
@RequestMapping("/admin/stoStockInPlan")
@Api(tags = "sto:stockInPlan", description = "车辆入库计划接口")
public class StoStockInPlanController {
    private static final Logger logger = LoggerFactory.getLogger(StoStockInPlanController.class);

    @Autowired
    StoStockInPlanDubboService stoStockInPlanDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    public ApiResult page(StoStockInPlanVO stoStockInPlanVO, @RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize) throws Exception {
        return ResponseUtil.ok(stoStockInPlanDubboService.findPageByVO(stoStockInPlanVO, pageNum, pageSize));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    @ApiImplicitParam(paramType = "query", name = "stockInPlanId", value = "主键id", required = true, dataType = "String")
    public ApiResult load(String stockInPlanId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(stoStockInPlanDubboService.getById(stockInPlanId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody StoStockInPlanVO stoStockInPlanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        stoStockInPlanVO.setIsManualIn(1);//是否手动入库为是
        stoStockInPlanDubboService.saveAndUpdate(stoStockInPlanVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/del")
    @ApiOperation(value = "作废")
    public ApiResult del(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
        stoStockInPlanDubboService.delAndReset(list, 1, loginInfo.getUserCode());
        return ResponseUtil.ok("success");
    }

    @PostMapping("/reset")
    @ApiOperation(value = "恢复")
    public ApiResult reset(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
        stoStockInPlanDubboService.delAndReset(list, 0, loginInfo.getUserCode());
        return ResponseUtil.ok("success");
    }

    @PostMapping("/bindVehicleRfid")
    @ApiOperation(value = "绑定车辆RFID")
    public ApiResult bindVehicleRfid(@RequestBody StoStockScanVO stoStockScanVO) throws Exception {
        String storageCode = stoStockInPlanDubboService.bindVehicleRfid(stoStockScanVO);
        return ResponseUtil.ok(storageCode);
    }

    @PostMapping("/setDriver")
    @ApiOperation(value = "分配司机")
    public ApiResult setDriver(@RequestBody StoStockInPlanDriverVO stoStockInPlanDriverVO) throws Exception {
        stoStockInPlanDubboService.setDriver(stoStockInPlanDriverVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/beginStockIn")
    @ApiOperation(value = "开始入库")
    public ApiResult beginStockIn(@RequestBody StoStockScanVO stoStockScanVO) throws Exception {
        stoStockInPlanDubboService.beginStockIn(stoStockScanVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/complete")
    @ApiOperation(value = "入库完成")
    public ApiResult complete(@RequestBody StoStockScanVO stoStockScanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        stoStockScanVO.setPartnerCode(loginInfo.getPartnerCode());
        stoStockInPlanDubboService.complete(stoStockScanVO);
        return ResponseUtil.ok("success");
    }

    @GetMapping("/stockInOutCount")
    @ApiOperation(value = "根据rfid查询六个月内出入库数量")
    @ApiImplicitParam(paramType = "query", name = "rfidNo", value = "rfidNo", required = true, dataType = "String")
    public ApiResult stockInOutCount(String rfidNo) throws Exception {
        return ResponseUtil.ok(stoStockInPlanDubboService.stockInOutCount(rfidNo));
    }

    @GetMapping("/planListByVin")
    @ApiOperation(value = "根据VIN码查询入库计划")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "vin", value = "vin", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "rfid", value = "rfid", required = true, dataType = "String")
    })
    public ApiResult planListByVin(String vin, String rfid) throws Exception {
        return ResponseUtil.ok(stoStockInPlanDubboService.planListByVin(vin, rfid));
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ApiResult export(StoStockInPlanVO stoStockInPlanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(stoStockInPlanDubboService.export(stoStockInPlanVO));
    }

    @GetMapping("/lateStockInPlan")
    @ApiOperation(value = "逾期未入库计划")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult lateStockInPlan(StoStockInPlanLateVO stoStockInPlanLateVO, Integer pageNum, Integer pageSize, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(stoStockInPlanDubboService.lateStockInPlan(stoStockInPlanLateVO, pageNum, pageSize));
    }

    @GetMapping("/rfidPlan")
    @ApiOperation(value = "通过RFID查询入库计划")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "rfid", value = "RFID号", required = true, dataType = "String")
    })
    public ApiResult lateStockInPlan(String rfid, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(stoStockInPlanDubboService.rfidPlan(rfid, loginInfo.getPartnerCode()));
    }

    @PostMapping("/returnIn")
    @ApiOperation(value = "退车入库")
    public ApiResult returnIn(@RequestBody StoStockReturnInVO stoStockReturnInVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        stoStockInPlanDubboService.returnIn(stoStockReturnInVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/imoprtPlan")
    @ApiOperation(value = "导入新增入库计划")
    public ApiResult imoprtPlan(@RequestBody List<StoStockInPlanImportVO> list) throws Exception {
        stoStockInPlanDubboService.imoprtPlan(list);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/inCharge")
    @ApiOperation(value = "开始充电")
    public ApiResult inCharge(@RequestBody StoStockScanVO stoStockScanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        stoStockScanVO.setUpdateBy(loginInfo.getUserCode());
        stoStockInPlanDubboService.inCharge(stoStockScanVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/outCharge")
    @ApiOperation(value = "完成充电")
    public ApiResult outCharge(@RequestBody StoStockScanVO stoStockScanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        stoStockScanVO.setUpdateBy(loginInfo.getUserCode());
        stoStockInPlanDubboService.outCharge(stoStockScanVO);
        return ResponseUtil.ok("success");
    }

}
